#!/usr/bin/python2.4


import time

import constants
import job_host_run
import utils


class JobHostRun(job_host_run.JobHostRun):

  __slots__ = ()

  @staticmethod
  def get_test_triage_info(test_view):
    return test_view.triage_info

  @staticmethod
  def set_test_triage_info(test_view, triage_info):
    test_view.triage_info = triage_info


class JobHostRunWithJobTest(job_host_run.JobHostRun):

  __slots__ = 'job'

  def __init__(self, job, tests):
    self.job = job
    super(JobHostRunWithJobTest, self).__init__(tests)

  def __getattr__(self, name):
    return getattr(self.job, name)

  def get_test_triage_info(self, test_view):
    return self.job.triage_info

  def set_test_triage_info(self, test_view, triage_info):
    self.job.triage_info = triage_info

  def get_test_labels(self, test_view):
    return self.job.labels

  def set_test_label(self, test_view, label_to_set):
    labels = [label
              for label in self.job.labels
              if label not in constants.ALL_TL_LABELS]
    if label_to_set:
      labels.append(label_to_set)
    if self.job.labels != labels:
      self.job.labels = labels

  @property
  def triage_info(self):
    return self.job.triage_info

  @property
  def labels(self):
    return self.job.labels

  @property
  def running_time(self):
    parsed_times = {}
    for job_name_type in ('queued', 'started', 'finished'):
      job_time = getattr(self, 'job_%s_time' % job_name_type)
      if job_time:
        parsed_times[job_name_type] = utils.parse_time(job_time)
      else:
        parsed_times[job_name_type] = time.time()

    return (parsed_times['finished'] - parsed_times['started'],
            parsed_times['finished'] - parsed_times['queued'])
